var grid;
function initGrid(){
	grid = $("#maingrid").ligerGrid({
		url:HTR.U('index/shops/pageQuery'),
		pageSize:HTR.pageSize,
		pageSizeOptions:HTR.pageSizeOptions,
		height:'99%',
        width:'100%',
        minColToggle:6,
        rownumbers:true,
        columns: [
            { display: '店铺编号', name: 'shopSn',isSort: false, width:100},
	        { display: '店铺名称', name: 'shopName',isSort: false, width:150,align:'left'},
	        { display: '店长姓名', name: 'keeperName',isSort: false, width:80},
	        { display: '店员姓名', name: 'clerkName',isSort: false, width:200,align:'left'},
	        { display: '联系电话', name: 'telephone',isSort: false, width:100},
	        { display: '店铺地址', name: 'address',isSort: false, align:'left'},
	        { display: '操作', name: 'op',isSort: false, width:120,render: function (rowdata, rowindex, value){
	            var h = "";
	            if(HTR.GRANT.DPGL_02)h += "<a href='javascript:toEdit(" + rowdata['shopId'] + ")'>修改</a> ";
	            if(HTR.GRANT.DPGL_03)h += "<a href='javascript:toDel(" + rowdata['shopId'] + ")'>删除</a> "; 
	            return h;
	        }}
        ]
    });
}
function loadGrid(){
	var params = HTR.getParams('.j-ipt');
	params.areaIdPath = HTR.ITGetAllAreaVals('areaId1','j-areas').join('_');
	grid.set('url',HTR.U('index/shops/pageQuery',params));
}
function initEdit(opts){
	if(opts.areaId>0){
		var areaIdPath = opts.areaIdPath.split("_");
    	$('#area_0').val(areaIdPath[0]);
    	var aopts = {id:'area_0',val:areaIdPath[0],childIds:areaIdPath,className:'j-areas',isRequire:true}
		HTR.ITSetAreas(aopts);		
	}	
}
function toDel(id){
	var box = HTR.confirm({content:"您确定要删除该店铺吗?",yes:function(){
   var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
   $.post(HTR.U('index/shops/del'),{id:id},function(data,textStatus){
   			  layer.close(loading);
   			  var json = HTR.toIndexJson(data);
   			  if(json.status=='1'){
   			    	HTR.msg("操作成功",{icon:1});
   			    	layer.close(box);
   		            grid.reload();
   			  }else{
   			    	HTR.msg(json.msg,{icon:2});
   			  }
   		});
    }});
}

function toEdit(id){
	location.href=HTR.U('index/shops/toEdit','id='+id);
}
function save(){
	$('#editFrom').isValid(function(v){
		if(v){
			var params = HTR.getParams('.ipt');
			params.areaId = HTR.ITGetAreaVal('j-areas');
			var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
		    $.post(HTR.U('index/shops/'+((params.shopId==0)?"add":"edit")),params,function(data,textStatus){
		    	layer.close(loading);
		    	var json = HTR.toIndexJson(data);
		    	if(json.status=='1'){
		    		HTR.msg("操作成功",{icon:1});
		    		location.href=HTR.U('index/shops/index');
		    	}else{
		    		HTR.msg(json.msg,{icon:2});
		    	}
		    });
		}
	});
}